from typing import Optional

from model.security_risk import SecurityRisk
from model.vulnerability import Vulnerability


def vuln_with_risk_note(note: Optional[str] = None):
    return Vulnerability(id="a", name="b", description="c", score=0, risk_note=note if note else "")


def test_dont_parse_risk_from_note():
    assert vuln_with_risk_note().get_risk() is None
    assert vuln_with_risk_note("").get_risk() is None
    assert vuln_with_risk_note(" ").get_risk() is None
    assert vuln_with_risk_note("n/a").get_risk() is None
    assert vuln_with_risk_note(":").get_risk() is None
    assert vuln_with_risk_note(" ; ").get_risk() is None
    assert vuln_with_risk_note("dont know").get_risk() is None
    assert vuln_with_risk_note("infolow").get_risk() is None
    assert vuln_with_risk_note("  lowball, probably").get_risk() is None
    assert vuln_with_risk_note("mediumrisk").get_risk() is None
    assert vuln_with_risk_note("higher than expected").get_risk() is None
    assert vuln_with_risk_note("CRITICALLY").get_risk() is None


def test_parse_risk_from_note_with_full_risk_as_first_word():
    assert vuln_with_risk_note("    INFORMATIONAL: some note").get_risk() == SecurityRisk.INFORMATIONAL
    assert vuln_with_risk_note("    loW  ").get_risk() == SecurityRisk.LOW
    assert vuln_with_risk_note(" medium because it is not high").get_risk() == SecurityRisk.MEDIUM
    assert vuln_with_risk_note("high,just because").get_risk() == SecurityRisk.HIGH
    assert vuln_with_risk_note("    CRITicAL").get_risk() == SecurityRisk.CRITICAL
    assert vuln_with_risk_note()


def test_parse_risk_from_note_with_partial_risk_as_first_word():
    assert vuln_with_risk_note("info; we are not affected").get_risk() == SecurityRisk.INFORMATIONAL
    assert vuln_with_risk_note("lo").get_risk() == SecurityRisk.LOW
    assert vuln_with_risk_note("  MED this needs further look   ").get_risk() == SecurityRisk.MEDIUM
    assert vuln_with_risk_note("  Hig because i got").get_risk() == SecurityRisk.HIGH
    assert vuln_with_risk_note("crit: holy moly ").get_risk() == SecurityRisk.CRITICAL


def test_has_risk_note():
    assert vuln_with_risk_note(" a ").has_risk_note()
    assert vuln_with_risk_note(";").has_risk_note()
    assert vuln_with_risk_note(" 1").has_risk_note()
    assert vuln_with_risk_note("A ").has_risk_note()


def test_has_no_risk_note():
    assert not vuln_with_risk_note().has_risk_note()
    assert not vuln_with_risk_note(" ").has_risk_note()
    assert not vuln_with_risk_note("  ").has_risk_note()
    assert not vuln_with_risk_note("\t").has_risk_note()
    assert not vuln_with_risk_note(" \n").has_risk_note()
    assert not vuln_with_risk_note(" \r ").has_risk_note()
    assert not vuln_with_risk_note("\n ").has_risk_note()
